import java.util.Arrays;

public class arr {
    public static void func1(int[] array) {
        array = new int[]{1, 2, 3};
    }

    public static void func2(int[] array) {
        array[0] = 199;
    }

    public static void main1(String[] args) {
        int[] array = {10, 20, 30};
        func1(array);
        func2(array);
        System.out.println(Arrays.toString(array));
    }

    public static int[] test() {
        int ret[] = {1, 2, 3, 4};
        return ret;
    }

    public static void main2(String[] args) {
        int[] tmp = test();
        System.out.println(Arrays.toString(tmp));
    }

    public static void main3(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        /**
         * 参数1：是要拷贝的数组
         * 参数2：是要拷贝的长度
         */
        //int[] copy1 = Arrays.copyOf(array,2*array.length);
        int[] copy2 = Arrays.copyOfRange(array, 2, 5);//[2,5)
        System.out.println(Arrays.toString(copy2));

        int[] copy = new int[array.length];

        System.arraycopy(array, 2, copy, 5, 3);

        System.out.println(Arrays.toString(copy));
    }

    public static void main4(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 18, 9};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));

        System.out.println(Arrays.binarySearch(array, 7));

        //System.out.println(binarySearch(array, 9));
    }

    public static void main5(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5, 6, 7, 18, 9};
        int[] array2 = {1, 2, 3, 4, 5, 6, 7, 18, 9, 20};

        boolean flg = Arrays.equals(array1, array2);
        System.out.println(flg);


        int[] array3 = new int[10];
        Arrays.fill(array3, 3, 5, -1);//[3,5)
        System.out.println(Arrays.toString(array3));

    }

    public static void main6(String[] args) {


        int[][] array = {{1, 2, 3}, {4, 5, 6}};

        for (int[] tmp : array) {
            for (int x : tmp) {
                System.out.print(x + " ");
            }
            System.out.println();
        }
        System.out.println();

        System.out.println(Arrays.toString(array));

    }


    public static void main7(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        System.out.println(avg(arr));
    }
    public static double avg(int[] arr) {
        int sum = 0;
        for (int x : arr) {
            sum += x;
        }
        return (double)sum / (double)arr.length;
    }


    public static void main8(String[] args) {
        int[] arr={1,4,3,5,7,2,8,9,0};
        System.out.println(find(arr,2));
    }

    public static int find(int[] arr,int num) {
        for(int i = 0; i <arr.length; i++ ){
            if(arr[i] == num){
                return i;
            }
        }
        return 0;//表示未找到
    }

    public static void main9(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        System.out.println(binarySearch(arr, 4));
    }
    public static int binarySearch(int[] arr, int toFind) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (toFind < arr[mid]) {
                right = mid - 1;
            } else if (toFind > arr[mid]) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;//未找到
    }

    public static void main(String[] args) {
        int[] arr = {8,9,10,3,7};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 1; j < arr.length-i; j++) {
                if (arr[j-1] > arr[j]) {
                    int tmp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = tmp;
                }
            }
        }
    }
}
